﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WRJClient.DAL;
using WRJClient.UI.Map;
using WRJClient.ViewModel;
using Newtonsoft.Json;
using Esri.ArcGISRuntime.Geometry;

namespace WRJClient.UI.SystemMonitor
{
    /// <summary>
    /// AirlineView.xaml 的交互逻辑
    /// </summary>
    public partial class AirlineView : UserControl
    {
        class MapPointViewModel
        {
            public string Name { get; set; }
            public double X { get; set; }
            public double Y { get; set; }
            public double Z { get; set; }

        }
        private AirlineViewModel _viewModel;
        public AirlineView(AirlineViewModel model)
        {
            InitializeComponent();
            MapManager.Instance.Init(baseMap);
            _viewModel = model;
            DataContext = _viewModel;
            Loaded += ControlCenterView_Loaded;
        }

        private void loadMap()
        {
            List<GeoPoint> points = JsonConvert.DeserializeObject<List<GeoPoint>>(_viewModel.StaticData.Waypoints);
            if (points != null)
            {
                List<MapPointViewModel> list = new List<MapPointViewModel>();
                for (int i = 0; i < points.Count; i++)
                {
                    var item = points.ElementAt(i);
                    list.Add(new MapPointViewModel { Name = item.index, X = item.longitude, Y = item.latitude, Z = item.altitude });
                }
                PointListView.ItemsSource = list;
            }
            _viewModel.AddGraphics();

            //BaseGraphic graphic = MapManager.Instance.GetGraphic(_viewModel.GetType().Name, GraphicPart.Base,
            //    _viewModel.StaticData.DbId);
            //if (graphic != null)
            //{
            //    try
            //    {
            //        baseMap.SetViewPoint(graphic.Geometry);
            //    }
            //    catch (Exception e)
            //    {
            //        MessageBox.Show(e.ToString());
            //    }
            //}
        }

        private void ControlCenterView_Loaded(object sender, RoutedEventArgs e)
        {
            loadMap();
        }
    }
}
